/*
 *  Copyright (C), 2015-2024
 *  FileName: instance_initialize
 *  Author: Tonight丶相拥
 *  Date: 5/16/24
 *  Description: 
 **/

part of 'flutter_tool.dart';

class InstanceInitialize {
  static final CustomLock<bool> _lock = CustomLock();
  static Future<void> initializeInstance({bool eventBus = true, bool keyboardObserve = false, bool lifeCircleObserve = false}) async{

    if (_lock.isInitLock && await _lock.awaitLockResult()) {
      debugLog('already successful initialized CustomInstanceManager: ==== ${DateTime.now()} ====');
      return;
    }

    _lock.lock();

    try {
      if (eventBus) {
        CustomInstanceManager.instance.setInstance(EventBus._internal());
      }
      if (keyboardObserve) {
        CustomInstanceManager.instance.setInstance(_KeyboardObserveTool._());
      }
      if (lifeCircleObserve) {
        CustomInstanceManager.instance.setInstance(_FlutterLifeCycleObserve._());
      }
      debugLog("""
        successful initialized CustomInstanceManager: ==== ${DateTime.now()} ====
        EventBus initialized: $eventBus
        KeyboardObserve initialized: $keyboardObserve
        FlutterLifeCircleObserve initialized: $lifeCircleObserve
      """);
      _lock.unlock(true);
    }catch(_) {
      _lock.unlock(false);
    }
    return;
  }

  static EventBus? get _eventBus => CustomInstanceManager.instance.getInstance<EventBus>();
}